package com.bop.dehong.util;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.base.dehong.util.LogUtils;
import com.base.dehong.util.PreferencesUtils;
import com.bop.dehong.config.Config;
import com.bop.dehong.lock.GesturePwdActivity;

/**
 * @author TangWenhan/YeFengyun
 * 锁屏唤醒
 *2015年12月7日上午11:15:05
 */
public class LockScreenUtil{
	private static LockScreenUtil instance ;
	
	public static LockScreenUtil getInstance(){
		if (instance == null) {
			instance = new LockScreenUtil();
		}
		return instance;
	}

	
	public void go2Backstage(Context context){
		Long exitTime = System.currentTimeMillis() / 1000;
		LogUtils.e("app进入后台，时间： " + exitTime);
		PreferencesUtils.putLong(context, "gesturetime", exitTime);
	}
	
	@SuppressLint("NewApi")
	public void go2Foreground(Context context){
		Long exitTime = PreferencesUtils.getLong(context, "gesturetime");
		LogUtils.e("app 从后台唤醒，进入前台 ，时间： " + exitTime);
		Long currentTime  = System.currentTimeMillis() / 1000;
		LogUtils.e("当前时间： " + currentTime);
		if (currentTime - exitTime > Config.TIMT_PSW) {
			verifyGesture(context);
		}
	}
	
	public boolean isAppOnForeground(Context context) {
		ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = context.getApplicationContext().getPackageName(); 
		List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
		if (appProcesses == null)
			return false; 
		for (RunningAppProcessInfo appProcess : appProcesses) // The name of the process that this object is associated with.
			if (appProcess.processName.equals(packageName) && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
				return true;
		return false;
	}
	
	public void verifyGesture(Context context){
		LogUtils.e("打开手势");
		String name = PreferencesUtils.getString(context, Config.USERNAME);
		String pwd = PreferencesUtils.getString(context, Config.GESTURE_PWD+name);
    	if (!TextUtils.isEmpty(pwd)) {
    		Intent intent = new Intent();
            intent.setClass(context, GesturePwdActivity.class);
            intent.putExtra(Config.GESTURE_ACTION, 0);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//关键的一步，设置启动模式
            context.startActivity(intent);
		}
	}
	
	public boolean isRunningForeground (Context context)  
	{  
	    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);  
	    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;  
	    String currentPackageName = cn.getPackageName();  
	    if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName()))  
	    {  
	        return true ;  
	    }  
	   
	    return false ;  
	}
	
}

